<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <el-form label-width="80px">
        <el-form-item label="用户名">
          <el-input disabled v-model="username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="changeSubmitForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="password" v-model="surePassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submitPasswordChange" type="success">确认</el-button>
          <el-button style="margin-left:100px">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { handleMessage } from "../../plugins";
export default {
  data() {
    return {
      changeSubmitForm: {},
      surePassword: "",
      username: ""
    };
  },
  methods: {
    async getUserInfo() {
      let res = await this.$http.get("teacherPageAPI/userInfo");
      this.username = res.data.username;
    },
    async submitPasswordChange() {
      if (!this.changeSubmitForm.password) {
        this.$message.error("请填写更改密码！");
        return;
      }
      if (!this.surePassword) {
        this.$message.error("请填写确认密码！");
        return;
      }
      if (this.changeSubmitForm.password != this.surePassword) {
        this.$message.error("密码和确认密码不符，请修改后再确认！");
        return;
      }
      let res = await this.$http.put(
        "teacherPageAPI/password",
        this.changeSubmitForm
      );
      handleMessage(this, res.data);
      this.$router.push("/");
      sessionStorage.clear();
    }
  },
  created() {
    this.getUserInfo();
  }
};
</script>

<style scoped>
.box-card {
  width: 480px;
}
</style>